package com.gary.springboot.component;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 自定义 国际化配置
 * @author pengrui
 * @date 2020/11/18 - 17:22
 *
 * 编写完自定义的国际化解析器时 还需要在configuration中将其加入到Spring容器中
 */
public class MyLocalResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String l = request.getParameter("l");
        // 设置默认的locale
        Locale locale = Locale.getDefault();
        //当获取的l参数不为null时 则根据传入参数设置国际化
        if (StringUtils.hasText(l)) {
            String[] split = l.split("_");
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
